package com.ldgame.entities;

import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.Color;

import com.ldgame.gfx.gfxelements.Sprite;

public class Building extends Entity {
	private Sprite flagSprite;
	
	public Building(Team team) {
		super(team);
		setSpeed(0);
		setRadius(64.0f);
		getSprite().setTexCoord(new Vector2f(32.0f, 64.0f));
		getSprite().setTexSize(new Vector2f(32.0f, 32.0f));
		getSprite().setColor(new Color(1.0f, 1.0f, 1.0f, 1.0f));
		flagSprite = new Sprite(position,
								16.0f,
								16.0f,
								new Vector2f(0.0f, 32.0f),
								new Vector2f(8.0f, 8.0f),
								(team == Team.TINY_HUMAN) ? new Color(0.0f, 0.0f, 0.6f, 1.0f) : new Color(0.6f, 0.0f, 0.0f, 1.0f));
	}
	
	@Override
	public void setPosition(Vector2f position) {
		super.setPosition(position);
		flagSprite.setPosition(position);
	}
	
	@Override
	public void render() {
		super.render();
		flagSprite.render();
	}
	
	@Override
	public void setTeam(Team team) {
		super.setTeam(team);
		flagSprite.setColor((team == Team.TINY_HUMAN) ? new Color(0.0f, 0.0f, 0.6f, 1.0f) : new Color(0.6f, 0.0f, 0.0f, 1.0f));
	}
	
	@Override
	public void handleCollisions(Entity entity) {
		if (entity.getPosition().x + entity.getRadius() > position.x - radius &&
			entity.getPosition().x - entity.getRadius() < position.x + radius &&
			entity.getPosition().y + entity.getRadius() > position.y - radius &&
			entity.getPosition().y - entity.getRadius() < position.y + radius) {
			if (Math.abs(entity.getPosition().x - position.x) > Math.abs(entity.getPosition().y - position.y)) {
				if (entity.getPosition().x > position.x) {
					entity.setTheta(0.0f);
				} else {
					entity.setTheta((float)Math.PI);
				}
			} else {
				if (entity.getPosition().y > position.y) {
					entity.setTheta(0.5f * (float)Math.PI);
				} else {
					entity.setTheta(1.5f * (float)Math.PI);
				}
			}
		}
	}

	public Sprite getFlagSprite() {
		return flagSprite;
	}

	public void setFlagSprite(Sprite flagSprite) {
		this.flagSprite = flagSprite;
	}
}
